package ad.ui 
{
	import ad.List;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * List
	 * @version ActionScript 3.0
	 * @author Adrian C. Miranda
	 * @mail ad@adrianmiranda.com.br
	 */
	public class ScrollList extends Sprite
	{
		protected var list:List;
		protected var holder:Sprite;
		protected var listMasker:Shape;
		protected var maskHeight:Number;
		protected var rows:int = 3;
		
		public function ScrollList(items:Array = null) 
		{
			list = new List(items);
			holder = new Sprite();
			listMasker = new Shape();
			makeList();
		}
		
		protected function draw():void 
		{
			// no yet implement 
		}
		
		public function addItem(item:Object):void 
		{
			list.addItem(item);
			makeList();
		}
		
		public function addItemAt(item:Object, index:int):void 
		{
			list.addItemAt(item, index);
			makeList();
		}
		
		public function removeItem(item:Object):void 
		{
			list.removeItem(item);
			makeList();
		}
		
		public function removeItemAt(index:int):void
		{
			list.removeItemAt(index);
			makeList();
		}
		
		public function removeAll():void
		{
			list.removeAll();
			unmakeList();
		}
		
		protected function makeList():void 
		{
			unmakeList();
			
			while (list.hasNext) 
			{
				var item:MovieClip = list.currentItem as MovieClip;
				item.y = (item.height * list.currentIndex);
				item.reference = list.currentIndex;
				item.buttonMode = true;
				item.mouseChildren = false;
				item.addEventListener(MouseEvent.CLICK, onItemSelect);
				holder.addChild(item);
				list.next();
			}
			
			/*maskHeight = item.height * rows;
			
			listMasker.graphics.clear();
			listMasker.graphics.beginFill(0x0);
			listMasker.graphics.drawRect(0, 0, item.width, maskHeight);
			listMasker.graphics.endFill();
			holder.mask = listMasker;*/
		}
		
		protected function unmakeList():void 
		{
			while (holder.numChildren > 0) 
			{
				holder.removeChildAt(0);
			}
		}
		
		private function onItemSelect(evt:MouseEvent):void 
		{
			dispatchEvent(new Event(Event.SELECT));
		}
		
	}

}